<template>
  <div class="ma-content-block">
    <!-- Tab 栏 jia-->
    <a-tabs v-model="activeTab" @change="handleTabChange" v-if="false">
      <a-tab-pane key="zh" title="中文"></a-tab-pane>
      <a-tab-pane key="en" title="英文"></a-tab-pane>
      <a-tab-pane key="ja" title="日语"></a-tab-pane>
      <a-tab-pane key="ko" title="韩语"></a-tab-pane>
    </a-tabs>

    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="政策/资讯标题" field="policy_title">
            <a-input v-model="searchForm.policy_title" placeholder="请输入政策/资讯标题" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
            <a-form-item field="policy_module" label="政策/资讯模块">
              <a-select
                  v-model="searchForm.policy_module"
                  :options="postData"
                  @change="handleModuleChange"
                  allow-clear
                  placeholder="请选择政策/资讯模块" />
            </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="policy_category" label="政策/资讯类别">
            <a-select
                v-model="searchForm.policy_category"
                :options="categoryData"
                
                allow-clear
                placeholder="请选择政策/资讯模块" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="是否置顶" field="is_top">
            <sa-select v-model="searchForm.is_top" dict="yes_or_no" placeholder="请选择是否置顶" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="是否推荐" field="is_recommend">
            <sa-select v-model="searchForm.is_recommend" dict="yes_or_no" placeholder="请选择是否推荐" allow-clear />
          </a-form-item>
        </a-col>

      </template>
      <template #is_top="{ record }">
        <sa-switch v-model="record.is_top" checkedText="是"  uncheckedText="否" @change="changeStatus($event, record.policy_id,1)"></sa-switch>
      </template>
      <template #is_recommend="{ record }">
        <sa-switch v-model="record.is_recommend" checkedText="是"  uncheckedText="否" @change="changeStatus($event, record.policy_id,2)"></sa-switch>
      </template>

      <template #sort_order="{ record }">
        <a-input-number
            v-model="record.sort_order"
            :min="0"
            style="width: 80px"
            @change="changeSortOrder(record.policy_id, record.sort_order)"
        />
      </template>
      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/policy'
import commonApi from '@/api/common'

// 引用定义
const crudRef = ref()
const editRef = ref()
const activeTab = ref('zh') // 默认选中中文jia
const postData = ref([])
const categoryData = ref([])
// 搜索表单
const searchForm = ref({
  policy_module: '',
  policy_category: '',
  is_top: '',
  is_recommend: '',
  policy_title: '',
  lang_code:''
})


// SaTable 基础配置
const options = reactive({
  api: (params) => api.getPageList({ ...params, ...searchForm.value }),
  pk: 'policy_id',
   recycleApi: (params) => api.recycleApi({ ...params, ...searchForm.value }),
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/policy/Policy/save'],
    func: async (params) => {
      const currentLangCode = searchForm.value.lang_code;
      editRef.value?.open()
      editRef.value?.setFormData({lang_code:currentLangCode})
    },
  },
  edit: {
    show: true,
    auth: ['/policy/Policy/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/policy/Policy/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/policy/Policy/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/policy/Policy/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  // { title: '排序', dataIndex: 'sort_order', width: 180, sortable: { sortDirections: ['ascend', 'descend'] } }//jia
  {
    title: '排序',
    dataIndex: 'sort_order',
    width: 180,
    slots: { customRender: 'sort_order' } // 新增插槽配置
  },
  { title: '政策/资讯主图', dataIndex: 'policy_image', type: 'image', width: 120 },
  { title: '政策/资讯标题', dataIndex: 'policy_title', width: 180 },
  { title: '政策/资讯模块', dataIndex: 'policy_module_text', width: 120 },
  { title: '政策/资讯类别', dataIndex: 'policy_category_text', width: 120 },
  { title: '是否置顶', dataIndex: 'is_top', dict: 'yes_or_no', width: 120 },
  { title: '是否推荐', dataIndex: 'is_recommend', dict: 'yes_or_no', width: 120 },
  // { title: '发布单位', dataIndex: 'issuing_unit', width: 180 },
  { title: '更新时间', dataIndex: 'update_time', width: 180 },
])
//jia
const changeStatus = async (status, id,type) => {
  const response = await api.changeStatus({ id, status,type})
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}
// 页面数据初始化
const initPage = async () => {
  searchForm.value.lang_code='zh'
  const postResp = await api.getDictData()
  postData.value = postResp.data

}
const handleModuleChange = async (value) => {
  searchForm.value.policy_category = '' // 清空类别选择
  try {
    // 根据选中的 policy_module 获取对应的 category 数据
    const category = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate', {
      classification_type: value
    })
    categoryData.value = category.data
  } catch (error) {
    console.error('获取类别数据失败', error)
    Message.error('获取类别数据失败')
  }
}

//jia
const handleTabChange = async (key) => {
  activeTab.value = key
  // 设置语言代码
  searchForm.value.lang_code = key
  // 刷新表格数据
  crudRef.value?.refresh()
}
// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}
//jia
const changeSortOrder = async (id,sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: id,
      sort_order: sortOrder
    });
    console.log(response,"fffffffffffffffffff")
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.log(error,"error")
    Message.error('排序修改失败1');
  }
}
// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})


</script>
